# -*- coding: utf-8 -*-

import datetime
import functools
import time
from decimal import *


def now_microisecond():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")


def now_millisecond():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]


def now_date():
    return time.strftime("%Y-%m-%d", time.localtime(time.time()))


def now_time():
    return time.strftime("%H:%M:%S", time.localtime(time.time()))


def now_():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))


def now_timestamp():
    """获取13位时间戳

    :return:
    """
    return str(int(time.time() * 1000))


def timestamp_to_datetime(timestamp):
    """时间戳转换成标准时间

    :param timestamp:13位时间戳
    :return:
    """

    time_array = time.localtime(int(timestamp))
    return time.strftime("%Y-%m-%d %H:%M:%S", time_array)


def days_difference(end_time):
    """计算相差的天数

    :param end_time: 2019-3-23 字符串
    :return:
    """
    start = datetime.datetime.now()
    end = datetime.datetime.strptime(end_time, "%Y-%m-%d")
    return (end - start).days


def seconds_difference(end_time):
    """计算相差的秒数

    :param end_time:2019-3-23 10:10:10字符串
    :return:
    """
    start = datetime.datetime.now()
    end = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
    return (end - start).seconds


def seconds_difference(end_time):
    """计算相差的分钟数

    :param end_time:2019-3-23 10:10:10字符串
    :return:
    """
    return (seconds_difference(end_time)) / 60


# 计算一个方法的执行时间并打印出来
def print_func_wrapper_spent(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start_time = now_()
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        exe_time = Decimal(end - start).quantize(Decimal('0.000'))
        print('StartTime: %s ,Func_Name: %s ,SpentTime: %s ' % (start_time, func.func_name, exe_time))
        return result

    return wrapper


# 计算一个方法的执行时间 返回tuple 第一个返回值是方法返回值,第二个是执行时间
def func_wrapper_spent(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        exe_time = Decimal(end - start).quantize(Decimal('0.000'))
        return result, str(exe_time)

    return wrapper
